package com.retirement.web.controller.csd;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.retirement.common.utils.SecurityUtils;
import com.retirement.csd.domain.EcharsServe;
import com.retirement.csd.domain.EcharsServeType;
import com.retirement.csd.service.IOdsServeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.retirement.common.annotation.Log;
import com.retirement.common.core.controller.BaseController;
import com.retirement.common.core.domain.AjaxResult;
import com.retirement.common.enums.BusinessType;
import com.retirement.csd.domain.OdsOrder;
import com.retirement.csd.service.IOdsOrderService;
import com.retirement.common.utils.poi.ExcelUtil;
import com.retirement.common.core.page.TableDataInfo;

/**
 * 服务订单Controller
 *
 * @author ruoyi
 * @date 2024-03-13
 */
@RestController
@RequestMapping("/csd/order")
public class OdsOrderController extends BaseController
{
    @Autowired
    private IOdsOrderService odsOrderService;
    @Autowired
    private IOdsServeService odsServeService;

    /**
     * 查询服务订单列表
     */
//    @PreAuthorize("@ss.hasPermi('csd:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(OdsOrder odsOrder)
    {
        if (!SecurityUtils.hasRole("admin"))
        {
            if(SecurityUtils.hasRole("common")){
                odsOrder.setOrdererId(SecurityUtils.getUserId());
            }else if(SecurityUtils.hasRole("serve")){
                odsOrder.setDeptId(SecurityUtils.getDeptId());
            }
        }
        startPage();
        List<OdsOrder> list = odsOrderService.selectOdsOrderList(odsOrder);
        return getDataTable(list);
    }

//    @PreAuthorize("@ss.hasPermi('csd:order:list')")
    @GetMapping("/echarsServe")
    public EcharsServe echarsServe(OdsOrder odsOrder)
    {
        List<EcharsServeType> list = odsOrderService.selectOdsOrderTypeNums();
        EcharsServe echarsServe = new EcharsServe();
        for (EcharsServeType serve : list){
            echarsServe.getServeType().add( odsServeService.selectOdsServeById(Long.parseLong(serve.getServeType())).getServeName());
            echarsServe.getNums().add(serve.getNums());
        }
        return echarsServe;
    }

    /**
     * 导出服务订单列表
     */
//    @PreAuthorize("@ss.hasPermi('csd:order:export')")
    @Log(title = "服务订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OdsOrder odsOrder)
    {
        List<OdsOrder> list = odsOrderService.selectOdsOrderList(odsOrder);
        ExcelUtil<OdsOrder> util = new ExcelUtil<OdsOrder>(OdsOrder.class);
        util.exportExcel(response, list, "服务订单数据");
    }

    /**
     * 获取服务订单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('csd:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(odsOrderService.selectOdsOrderById(id));
    }

    /**
     * 新增服务订单
     */
//    @PreAuthorize("@ss.hasPermi('csd:order:add')")
    @Log(title = "服务订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OdsOrder odsOrder)
    {
        return toAjax(odsOrderService.insertOdsOrder(odsOrder));
    }

    /**
     * 修改服务订单
     */
//    @PreAuthorize("@ss.hasPermi('csd:order:edit')")
    @Log(title = "服务订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OdsOrder odsOrder)
    {
        return toAjax(odsOrderService.updateOdsOrder(odsOrder));
    }

    /**
     * 删除服务订单
     */
//    @PreAuthorize("@ss.hasPermi('csd:order:remove')")
    @Log(title = "服务订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(odsOrderService.deleteOdsOrderByIds(ids));
    }
}
